#include "trueapi.h"
#include "config.h"
#include "constvariable.h"
#include <QDate>

TrueApi::TrueApi()
{

}

TrueApi::~TrueApi()
{
}

QByteArray TrueApi::sendDateChange(QString time)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = currentData;
    sendDataInfo.msg.insert("date",time);
    QByteArray data = structToJson(sendDataInfo);
}

QByteArray TrueApi::sendAddrAndGroupnumber(int addr, int groupnumber)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = addrAndGroupnumber;
    sendDataInfo.data.insert("addr",addr);
    sendDataInfo.data.insert("group_number",groupnumber);
    QByteArray data = structToJson(sendDataInfo);
}

// sendGroupNumber
//
//	@Desc: 发送分组信息
//	@param groupNumber
QByteArray TrueApi::sendGroupNumber(int groupNumber)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = UserGrouping;
    sendDataInfo.data.insert("group_number",groupNumber);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//sendYuanXin
//
//  @desc:发送原编码和新编码
//  @param
QByteArray TrueApi::sendYuanXin(int old_addr, int new_addr)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = RewriteTargetId;
    sendDataInfo.data.insert("old_addr",old_addr);
    sendDataInfo.data.insert("new_addr",new_addr);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//sendBegin
//
//@desc:发送开始校准信息
//@param
QByteArray TrueApi::sendBegin(int flag)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = OpenCalibrationTargetById;
    sendDataInfo.data.insert("addr",flag);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//sendOver
//
//  @deac:发送结束校准信息
//  @param
QByteArray TrueApi::sendOver(int flag)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = CloseCalibrationTargetById;
    sendDataInfo.data.insert("addr",flag);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//bindInfo
//
//@desc:获取绑定信息
//@param
QByteArray TrueApi::bindInfo(int addr,const QString& name,const QString& idCard)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = BindInfo;
    sendDataInfo.msg.insert("user_name",name);
    sendDataInfo.msg.insert("identification_number",idCard);
    sendDataInfo.data.insert("addr",addr);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//sendId
//
//@desc:发送系统ID
//@param
QByteArray TrueApi::sendId(int addr)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = ChangeSystemId;
    sendDataInfo.data.insert("SystemID",addr);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

QByteArray TrueApi::sendUporDownmsg(int code)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = upORdown;
    sendDataInfo.data.insert("up_down",code);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//requestClearAll
//
//@desc:获取清除全部靶标信息
//@param
QByteArray TrueApi::requestClearAll()
{
Send_Info sendDataInfo;
sendDataInfo.code = ClearTarget;
sendDataInfo.data.insert("addr",-1);
QByteArray data = structToJson(sendDataInfo);
return data;
}

//requestClearOnly
//
//@desc:获取仅清楚一个靶标信息
//@param
QByteArray TrueApi::requestClearOnly(int index2)
{
Send_Info sendDataInfo;
sendDataInfo.code = ClearTarget;
sendDataInfo.data.insert("addr",index2);
QByteArray data = structToJson(sendDataInfo);
return data;
}





//requestState
//
//@desc:获取状态信息
//@param
QByteArray TrueApi::requestState()
{
    Send_Info sendDataInfo;
    sendDataInfo.code = GetTargetState;
    QByteArray dataState = structToJson(sendDataInfo);
    return dataState;
}

//requestBattery
//
//@desc:获取电量信息
//@param
QByteArray TrueApi::requestBattery()
{
    Send_Info sendDataInfo;
    sendDataInfo.code = GetVoltage;
    QByteArray dataBattery  = structToJson(sendDataInfo);
    return dataBattery;
}

//sendSearchData
//
//  @deac:发送查找数据信息指令
//  @param   fenzu,bahao,time
QByteArray TrueApi::requestSearchDataApi(int groupNumber, int addr, const QString& time)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = GetShowInfo;
    sendDataInfo.msg.insert("date",time);
    sendDataInfo.data.insert("addr",addr);
    sendDataInfo.data.insert("group_number",groupNumber);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

//QByteArray TrueApi::sendHoles(int msg,int group_number)
//{
//    QDate m_date = QDate::currentDate();
//    QString currentDate = m_date.toString("yyyy-MM-dd");
//    Send_Info sendDataInfo;
//    sendDataInfo.code = GetTargetInfo;
//    sendDataInfo.msg.insert("date",currentDate);
//    sendDataInfo.data.insert("addr",msg);
//    sendDataInfo.data.insert("group_number",group_number);
//    QByteArray data = structToJson(sendDataInfo);
//    return data;
//}


//QByteArray TrueApi::requestDate(int addr)
//{
//    return api->requestDateApi(addr);
//}



//sendXandY
//
//@desc:发送校准后X和Y的信息
//@param
QByteArray TrueApi::sendXandY(int addr,int x,int y)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = TwoCheck;
    sendDataInfo.data.insert("addr",addr);
    sendDataInfo.data.insert("x",x);
    sendDataInfo.data.insert("y",y);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}



//QByteArray TrueApi::searchCurrentDateApi(int groupnumber,int addr)
//{
//    return api->searchCurrentDateApi(groupnumber,addr);
//}

QByteArray TrueApi::pushTargetStateInfoApi()
{
    Send_Info sendDataInfo;
    sendDataInfo.code = GetTargetState;
    QReadLocker readLocker(&StateOfTargetMapRwLock);
    foreach(int key, StateOfTargetMap.keys())
    {
        int value = StateOfTargetMap.value(key);
        QString addr = QString::number(key);
        sendDataInfo.data.insert(addr,value);
    }
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

QByteArray TrueApi::pushTargetVoltageInfoApi()
{
    Send_Info sendDataInfo;
    sendDataInfo.code = GetVoltage;
    QReadLocker readLocker(&StateOfTargetMapRwLock);
    foreach(int key, VoltageOfTargetMap.keys())
    {
        int value = VoltageOfTargetMap.value(key);
        QString addr = QString::number(key);
        sendDataInfo.data.insert(addr,value);
    }

    QByteArray data = structToJson(sendDataInfo);
    return data;
}

QByteArray TrueApi::currentTargetBullet(const OneTaregtBullet &oneTaregtBullet)
{
    Send_Info sendDataInfo;
    sendDataInfo.code = CurrentBullet;
    sendDataInfo.data.insert("addr",oneTaregtBullet.addr);
    sendDataInfo.data.insert("x",oneTaregtBullet.x);
    sendDataInfo.data.insert("y",oneTaregtBullet.y);
    sendDataInfo.data.insert("cn",oneTaregtBullet.cylinder_number);
    sendDataInfo.data.insert("dir",oneTaregtBullet.direction);
    QByteArray data = structToJson(sendDataInfo);
    return data;
}

QByteArray TrueApi::pushSearchDateApi()
{
    Send_Info sendDataInfo;
    sendDataInfo.code = scoreBullet;
    QByteArray data = structToJson(sendDataInfo);
    return data;
}
